package com.xinchuang.service.targetSystem.Impl.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultEnum;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.elseParam.Ele;
import com.xinchuang.entity.targetSystem.object.SystemParamAdd;
import com.xinchuang.mapper.targetSystem.SystemMasterMapper;
import com.xinchuang.mapper.targetSystem.SystemSortMapper;
import com.xinchuang.mapper.targetSystem.SystemsMasterMapper;
import com.xinchuang.mapper.targetSystem.config.SystemParamMapper;
import com.xinchuang.entity.targetSystem.config.SystemConfig;
import com.xinchuang.entity.targetSystem.config.SystemParam;
import com.xinchuang.entity.targetSystem.system.SystemMaster;
import com.xinchuang.entity.targetSystem.system.SystemsMaster;
import com.xinchuang.service.elses.EleService;
import com.xinchuang.service.targetSystem.config.SystemParamService;
import com.xinchuang.util.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemParamServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2023-08-07 14:15
 * @Verion V1.0
 */
@Service
public class SystemParamServiceImpl implements SystemParamService {

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0, 0);

    @Resource
    private SystemParamMapper systemParamMapper;

    @Resource
    private SystemMasterMapper systemMasterMapper;

    @Resource
    private SystemsMasterMapper systemsMasterMapper;

    @Resource
    private SystemSortMapper systemSortMapper;

    @Resource
    private EleService eleService;

    @Override
    public Result addOrUpdateSystemParam(SystemParamAdd systemParamAdd) {
        SystemParam param = systemParamMapper.selectSystemParamOnly(systemParamAdd.getKey());
        if (ObjectUtil.isNotNull(param) && !param.getId().equals(systemParamAdd.getId())) {
            return ResultUtil.error(ResultEnum.COMMON_KEY_NAME_EXIST);
        }
        SystemParam systemParam = new SystemParam();
        systemParam.setId(systemParamAdd.getId());
        systemParam.setValue(systemParamAdd.getValue());
        systemParam.setRemark(systemParamAdd.getRemark());
        systemParam.setType(systemParamAdd.getType());
        if (StringUtils.isBlank(systemParam.getId()) || systemParam.getId().equals("0")) {
            if (ObjectUtil.isNotNull(param)) {
                return ResultUtil.error(ResultEnum.COMMON_KEY_NAME_EXIST);
            }
            systemParam.setId(snow.nextId() + "");
            systemParam.setKeyValue(systemParamAdd.getKey());
            systemParam.setKeyName(systemParamAdd.getKeyName());
            systemParamMapper.addSystemParam(systemParam);
        } else if (ObjectUtil.isNotNull(param) && !param.getId().equals(systemParam.getId())) {
            return ResultUtil.error(ResultEnum.COMMON_KEY_NAME_EXIST);
        } else {
            systemParamMapper.updateSystemParam(systemParam);
        }
        return ResultUtil.success();
    }

    @Override
    public Result deleteSystemParam(String id) {
        if (systemParamMapper.deleteSystemParam(id) > 0) {
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(ResultEnum.COMMON_EMPTY);
    }

    @Override
    public Result selectSystemParam(Integer pageNum, Integer pageSize, Boolean isPull) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize, "create_time DESC");
        List<SystemParam> list = systemParamMapper.selectSystemParamList();
        PageInfo<SystemParam> pageInfo = new PageInfo<>(list);
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (SystemParam systemParam : pageInfo.getList()) {
                map = new HashMap<>();
                map.put("id", systemParam.getId());
                map.put("key", systemParam.getKeyValue());
                map.put("keyName", systemParam.getKeyName());
                map.put("value", systemParam.getValue());
                map.put("remark", systemParam.getRemark());
                resultList.add(map);
            }
            resultMap.put("dataList", resultList);
        } else {
            resultMap.put("dataList", new ArrayList<>());
        }
        resultMap.put("pageNum", pageInfo.getPageNum());
        resultMap.put("pageSize", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("pageAll", pageInfo.getPages());
        return ResultUtil.success(resultMap);
    }

    @Override
    public Result selectSystemParamByKey(String key) {
        SystemParam systemParam = systemParamMapper.selectSystemParamOnly(key);
        SystemParamAdd add = new SystemParamAdd();
        add.setId(systemParam.getId());
        add.setKey(systemParam.getKeyValue());
        add.setKeyName(systemParam.getKeyName());
        add.setValue(systemParam.getValue());
        add.setRemark(systemParam.getRemark());
        add.setType(systemParam.getType());
        return ResultUtil.success(add);
    }

    @Override
    public Result selectSystemParamById(String id) {
        SystemParam systemParam = systemParamMapper.selectSystemParamIdOnly(id);
        SystemParamAdd add = new SystemParamAdd();
        add.setId(systemParam.getId());
        add.setKey(systemParam.getKeyValue());
        add.setKeyName(systemParam.getKeyName());
        add.setValue(systemParam.getValue());
        add.setRemark(systemParam.getRemark());
        add.setType(systemParam.getType());
        return ResultUtil.success(add);
    }

    @Override
    public Result initSystemSort() {
        SystemMaster one = systemMasterMapper.getOne();
        if (ObjectUtil.isNotNull(one)) {
            if (null == one.getSort()) {
                List<SystemMaster> list = systemMasterMapper.selectSystemList(null, null);
                for (int i = 0; i < list.size(); i++) {
                    SystemMaster systemMaster =  list.get(i);
                    systemSortMapper.updateSystem(systemMaster.getSId(), i + 1);
                }
            }
        }

        SystemsMaster one1 = systemsMasterMapper.getOne();
        if (ObjectUtil.isNotNull(one1)) {
            if (null == one1.getSort()) {
                List<SystemsMaster> systemsMasters = systemsMasterMapper.selectSystemList(null, null);
                for (int i = 0; i < systemsMasters.size(); i++) {
                    SystemsMaster systemsMaster =  systemsMasters.get(i);
                    systemSortMapper.updateSystems(systemsMaster.getSysId(), i + 1);
                }
            }
        }
        return null;
    }

    @Override
    public Result initSytemParam() {
        List<SystemParam> params = systemParamMapper.selectSystemParamList();
        if (CollectionUtil.isEmpty(params)) {
            String[] key = {
                    "systemName",
                    "lineWidth",
                    "lineFar",
                    "lineTextFar"
            };
            String[] keyName = {
                    "系统名称",
                    "关系线粗细",
                    "关系线高度",
                    "关系线文字高度"
            };
            String[] value = {
                    "识别MB",
                    "1",
                    "2000000",
                    "300000"
            };
            String[] remark = {
                    "系统名称",
                    "关系线粗细",
                    "关系线高度",
                    "关系线文字高度"
            };
            for (int i = 0; i < key.length; i++) {
                SystemParam systemParam = new SystemParam();
                systemParam.setId(snow.nextId() + "");
                systemParam.setKeyValue(key[i]);
                systemParam.setKeyName(keyName[i]);
                systemParam.setValue(value[i]);
                systemParam.setRemark(remark[i]);
                systemParamMapper.addSystemParam(systemParam);
            }
        }
        return null;
    }

    @Override
    public Result initElement() {
        if (eleService.count() == 0) {
            String[] strings = {"MB概况","地位作用","组成结构","YH部位","周围情况","定位精度","地理环境","防御部署","气象水文","整编说明","成果审定说明","成果依据资料","成果制作单位"};
            List<Ele> list = new ArrayList<>();
            for (int i = 0; i < strings.length; i++) {
                Ele ele = new Ele();
                ele.setEleId(i);
                ele.setEleName(strings[i]);
                list.add(ele);
            }
            eleService.saveBatch(list);
        }
        return null;
    }
}